import 'package:flutter/material.dart';

import '../../config/kq_global.dart';
import '../../resources/images.dart';
import '../../resources/kq_theme_colors.dart';
import '../../theme/kq_theme_manager.dart';
import '../formItem/kq_radio_item_view.dart';
import 'kq_ink_well.dart';

/// 单选按钮组
class KqRadioButtonGroup extends StatefulWidget {
  /// 单选控件的数据，如果要么默认选中某个，[checkedId] 赋值成选中的 [RadioData]的[itemId]
  final List<RadioData> radioData;

  /// 单选按钮之间的距离，默认12
  final double? btnSpacing;

  /// 默认选中的itemId
  final String? defCheckedId;

  /// 是否可编辑
  final bool editable;

  /// 文字颜色
  final Color? textColor;

  /// 单选组件单选点击回调
  final Function(RadioData radioData)? onRadioChanged;

  const KqRadioButtonGroup(
      {Key? key,
      required this.radioData,
      this.editable = true,
      this.btnSpacing,
      this.textColor,
      this.defCheckedId,
      this.onRadioChanged})
      : super(key: key);

  @override
  KqRadioButtonGroupState createState() => KqRadioButtonGroupState();
}

class KqRadioButtonGroupState extends State<KqRadioButtonGroup> {
  String? checkedId;

  @override
  void initState() {
    checkedId = widget.defCheckedId;
    super.initState();
  }

  @override
  void didUpdateWidget(covariant KqRadioButtonGroup oldWidget) {
    checkedId = widget.defCheckedId;
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    return ScrollConfiguration(
      behavior: ScrollConfiguration.of(context).copyWith(scrollbars: false),
      child: SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: Row(
          children: _renderRadioButtonList(),
        ),
      ),
    );
  }

  List<Widget> _renderRadioButtonList() {
    if (!widget.editable) {
      //非编辑模式，只显示选中按钮的文字
      for (var data in widget.radioData) {
        if (data.itemId == checkedId) {
          return [
            Text(
              data.item,
              style: TextStyle(
                  fontSize: KqThemeManager.instance
                          .getConfig()
                          .formConfig
                          .contentFontSize ??
                      16,
                  color: KqThemeColors.text59),
            )
          ];
        }
      }
      return [];
    }
    return widget.radioData
        .map((e) => Row(
              children: [
                _renderRadioButtonItem(e),
                if (widget.radioData.indexOf(e) < widget.radioData.length - 1)
                  SizedBox(
                    width: widget.btnSpacing ?? 12,
                  )
              ],
            ))
        .toList();
  }

  Widget _renderRadioButtonItem(RadioData data) {
    return KqInkWell(
      radius: 4,
      onTap: () {
        setState(() {
          checkedId = data.itemId;
          //回调
          widget.onRadioChanged?.call(data);
        });
      },
      borderWidth: 0,
      child: Padding(
        padding: const EdgeInsets.only(left: 5, top: 5, bottom: 5, right: 5),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Icon(
              data.itemId == checkedId
                  ? Icons.radio_button_on
                  : Icons.radio_button_off,
              size: 20,
              color: data.itemId == checkedId
                  ? KqThemeManager.getCommonConfig().mainLightColor
                  : KqThemeColors.textBF,
            ),
            Container(
              margin: const EdgeInsets.only(left: 5),
              child: Text(
                data.item,
                style: TextStyle(
                    fontSize: KqThemeManager.instance
                            .getConfig()
                            .formConfig
                            .contentFontSize ??
                        16,
                    color: widget.textColor ?? KqThemeColors.text59),
              ),
            )
          ],
        ),
      ),
    );
  }

}
